package idea.iquest.editor;


import idea.apptopcomponents.AppTopComponents;
import idea.iquest.explorer.IQuestionnairesTopComponent;
import idea.model.dbmanager.QueryManager;
import idea.model.qnrlibrary.Questionnaires;
import idea.model.qnrlibrary.ReportingPeriods;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import org.idea.utils.sfo.TreeTopComponent;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.LifecycleManager;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.StatusDisplayer;
import org.openide.awt.UndoRedo;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import org.openide.util.lookup.InstanceContent;
import org.openide.windows.WindowManager;


/**
 * Opening Editor Top Component Displays the six criteria to search on to bring
 * up a table of Questionnaires.
 *
 *
 */
@ConvertAsProperties(
        dtd = "-//idea.iquest.editor//OpeningEditor//EN" ,
        autostore = false )
@TopComponent.Description(
        preferredID = "OpeningEditorTopComponent" ,
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS )
@TopComponent.Registration( mode = "explorer" , openAtStartup = true )
@ActionID( category = "Window" , id = "idea.iquest.editor.OpeningEditorTopComponent" )
@ActionReferences(
        {
    @ActionReference( path = "Menu/Window" , position = 333 ) ,
    @ActionReference( path = "Menu/File/Open" , position = 1200 )
} )
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_OpeningEditorAction" ,
        preferredID = "OpeningEditorTopComponent" )
@Messages(
        {
    "CTL_OpeningEditorAction=Opening Editor" ,
    "CTL_OpeningEditorTopComponent=Opening Editor Window" ,
    "HINT_OpeningEditorTopComponent=This is the Opening Editor window"
} )
public final class OpeningEditorTopComponent extends TopComponent implements LookupListener
{

    private Lookup.Result result = null;
    private UndoRedo.Manager urMgr = new UndoRedo.Manager();
    private InstanceContent instanceContent = new InstanceContent();
    private Questionnaires questionnaires;
    private AppTopComponents appTCs;


    /**
     * xtor.
     */
    public OpeningEditorTopComponent()
    {
        initComponents();
        setName( Bundle.CTL_OpeningEditorTopComponent() );
        setToolTipText( Bundle.HINT_OpeningEditorTopComponent() );

        initComboBoxes();
        appTCs = new AppTopComponents();
//        appTCs.closeCrudTCs();
//        TopComponent treeTC = appTCs.getTreeTC();
//        treeTC.close();

        putClientProperty( "print.printable" , Boolean.TRUE );
        putClientProperty( "print.name" , "Ed's Questionnaire Criteria" );

        txtTitle.getDocument().addUndoableEditListener( urMgr );
        txtSubTitle.getDocument().addUndoableEditListener( urMgr );
        txtCreatedBy.getDocument().addUndoableEditListener( urMgr );

        StatusDisplayer.getDefault().setStatusText( "This is Ed's status!!!" );
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
        lblMainTitle = new javax.swing.JLabel();
        lblMainTitle1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        cbSurvey = new javax.swing.JComboBox();
        lblSurvey = new javax.swing.JLabel();
        lblRefPeriod = new javax.swing.JLabel();
        cbRefPeriod = new javax.swing.JComboBox();
        lblFormNumber = new javax.swing.JLabel();
        cbFormNumber = new javax.swing.JComboBox();
        lblCreatedBy = new javax.swing.JLabel();
        txtCreatedBy = new javax.swing.JTextField();
        txtSubTitle = new javax.swing.JTextField();
        lblSubTitle = new javax.swing.JLabel();
        txtTitle = new javax.swing.JTextField();
        lblTitle = new javax.swing.JLabel();
        lblQuestionnaires = new java.awt.Label();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        btnSearch = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();

        lblMainTitle.setFont(new java.awt.Font("Lucida Grande", 1, 24)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblMainTitle, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblMainTitle.text")); // NOI18N

        lblMainTitle1.setFont(new java.awt.Font("Lucida Grande", 1, 24)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblMainTitle1, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblMainTitle1.text")); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        cbSurvey.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[no survey]", "Survey 1", "Survey 2", "Survey 3" }));
        cbSurvey.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbSurveyItemStateChanged(evt);
            }
        });

        lblSurvey.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblSurvey, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblSurvey.text")); // NOI18N

        lblRefPeriod.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblRefPeriod, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblRefPeriod.text")); // NOI18N

        cbRefPeriod.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[no ref period]", "Ref Period 1", "Ref Period 2", "Ref Period 3", "Ref Period 4" }));
        cbRefPeriod.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbRefPeriodItemStateChanged(evt);
            }
        });

        lblFormNumber.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblFormNumber, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblFormNumber.text")); // NOI18N

        cbFormNumber.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "[no form number]", "Form Number 1", "Form Number 2", "Form Number 3", "Form Number 4" }));

        lblCreatedBy.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblCreatedBy, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblCreatedBy.text")); // NOI18N

        txtCreatedBy.setText(org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.txtCreatedBy.text")); // NOI18N
        txtCreatedBy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCreatedByActionPerformed(evt);
            }
        });

        txtSubTitle.setText(org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.txtSubTitle.text")); // NOI18N

        lblSubTitle.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblSubTitle, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblSubTitle.text")); // NOI18N

        txtTitle.setText(org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.txtTitle.text")); // NOI18N

        lblTitle.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblTitle, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblTitle.text")); // NOI18N

        lblQuestionnaires.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        lblQuestionnaires.setText(org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.lblQuestionnaires.text")); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblRefPeriod)
                            .addComponent(lblSurvey)
                            .addComponent(cbSurvey, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbRefPeriod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(33, 33, 33)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lblTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblSubTitle)
                            .addComponent(lblCreatedBy)
                            .addComponent(txtSubTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                            .addComponent(txtTitle)
                            .addComponent(txtCreatedBy, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(24, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblFormNumber)
                            .addComponent(cbFormNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblQuestionnaires, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblQuestionnaires, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSurvey)
                    .addComponent(lblTitle))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbSurvey, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblRefPeriod)
                    .addComponent(lblSubTitle))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbRefPeriod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSubTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblFormNumber)
                    .addComponent(lblCreatedBy))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbFormNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCreatedBy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34))
        );

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.jLabel1.text")); // NOI18N
        jLabel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        org.openide.awt.Mnemonics.setLocalizedText(btnSearch, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.btnSearch.text")); // NOI18N
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btnClear, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.btnClear.text")); // NOI18N
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btnExit, org.openide.util.NbBundle.getMessage(OpeningEditorTopComponent.class, "OpeningEditorTopComponent.btnExit.text")); // NOI18N
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(btnSearch)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addComponent(btnClear)
                .addGap(36, 36, 36)
                .addComponent(btnExit)
                .addGap(53, 53, 53))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(24, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSearch)
                    .addComponent(btnClear)
                    .addComponent(btnExit))
                .addGap(18, 18, 18))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblMainTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(lblMainTitle1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(14, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(lblMainTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblMainTitle1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(38, Short.MAX_VALUE))
        );

        jScrollPane1.setViewportView(jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    /**
     * Search button action. Searches for Questionnaires that match the chosen
     * criteria
     *
     * @param evt-- ActionEvent
     */
    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        appTCs.closeCrudTCs();
        TopComponent treeTC = appTCs.getTreeTC();
        treeTC.close();
        String refPeriod = null;
        Object item = cbRefPeriod.getSelectedItem();
        /**
         * JPA has a quirk that, whereas the RefPeriod in the questionnaires
         * table is a GUID (string), it sometimes reads it as the
         * ReportingPeriods object the GUID points to.
         */
        if ( item instanceof ReportingPeriods )
        {
            ReportingPeriods rp = (ReportingPeriods) item;
            refPeriod = rp.getRefPeriod();
        }
        else if ( item instanceof String )
        {
            refPeriod = (String) item;

        }
        // create and run the query based on the six criteria
        List<Questionnaires> resultList = QueryManager.buildQnrQuery(
                (String) cbSurvey.getSelectedItem() ,
                (String) txtTitle.getText() ,
                (String) txtSubTitle.getText() ,
                (String) cbFormNumber.getSelectedItem() ,
                refPeriod ,
                (String) txtCreatedBy.getText() );

        // Go get the Questionnaires top component and display the result set.

        IQuestionnairesTopComponent iqtc = appTCs.getQnrTC();
        //(IQuestionnairesTopComponent) WindowManager.getDefault().findTopComponent( "IQuestionnairesTopComponent" );
        iqtc.displayQuestionnaires( resultList );

        openTC( iqtc );


        //associateLookup( ExplorerUtils.createLookup( iqtc.getExplorerManager() , getActionMap() ) );


    }//GEN-LAST:event_btnSearchActionPerformed


    /**
     * Clear button action
     *
     * @param evt -- ActionEvent
     */
    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
        clearTextFields();
        initComboBoxes();
        appTCs.closeCrudTCs();
        appTCs.closeSfoTCs();

//        TopComponent treeTC = appTCs.getTreeTC();
//        treeTC.close();

    }//GEN-LAST:event_btnClearActionPerformed


    /**
     * Clear the three text fields
     */
    private void clearTextFields()
    {
        txtTitle.setText( "" );
        txtSubTitle.setText( "" );
        txtCreatedBy.setText( "" );
    }


    /**
     * Exit the application.
     *
     * @param evt
     */
    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        LifecycleManager.getDefault().exit();
    }//GEN-LAST:event_btnExitActionPerformed


    /**
     * If a different survey is picked in the combo box, populate the other two
     * combo boxes correctly
     *
     * @param evt -- ItemEvent
     */
    private void cbSurveyItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbSurveyItemStateChanged

        populateRefPeriodComboBox( (String) cbSurvey.getSelectedItem() );

        Object item = cbRefPeriod.getSelectedItem();   // that pesky ReportingPeriods vs. String object again
        populateFormNumComboBox( (String) cbSurvey.getSelectedItem() , item );

        clearTextFields();
    }


    /**
     * populate the ref period combo box with the appropriate ref periods for
     * that survey.
     *
     * @param survey -- String
     */
    private void populateRefPeriodComboBox( String survey )
    {
        List<String> refPds = QueryManager.getRefPeriods( survey );

        // add an empty ref period so user can search on all
        refPds.add( refPds.size() , "" );
        //refPds.add( 0 , "" );
        cbRefPeriod.setModel( new DefaultComboBoxModel( refPds.toArray() ) );
    }


    /**
     * populate the form number combo box with the appropriate ref periods for
     * that survey / ref period.
     *
     * @param survey -- String
     * @param refPd -- note it is an Object, could be either String or
     * ReportingPeriods
     */
    private void populateFormNumComboBox( String survey , Object refPd )
    {
        List<String> formNums = new ArrayList<>();
        if ( refPd instanceof String )
        {
            formNums = QueryManager.getFormNumbers( survey , (String) refPd );
        }
        else if ( refPd instanceof ReportingPeriods )
        {
            formNums = QueryManager.getFormNumbers( survey , (ReportingPeriods) refPd );
        }
        // add an empty form num so user can search on all
        //formNums.add( 0 , "" );
        formNums.add( formNums.size() , "" );
        cbFormNumber.setModel( new DefaultComboBoxModel( formNums.toArray() ) );
    }//GEN-LAST:event_cbSurveyItemStateChanged


    /**
     * When the Ref Period combo box is changed, populate the Form Number cb
     * correctly.
     *
     * @param evt -- ItemEvent
     */
    private void cbRefPeriodItemStateChanged(java.awt.event.ItemEvent evt)//GEN-FIRST:event_cbRefPeriodItemStateChanged
    {//GEN-HEADEREND:event_cbRefPeriodItemStateChanged
        populateFormNumComboBox( (String) cbSurvey.getSelectedItem() , cbRefPeriod.getSelectedItem() ); // second arg is an Object
    }//GEN-LAST:event_cbRefPeriodItemStateChanged

    private void txtCreatedByActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_txtCreatedByActionPerformed
    {//GEN-HEADEREND:event_txtCreatedByActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCreatedByActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cbFormNumber;
    private javax.swing.JComboBox cbRefPeriod;
    private javax.swing.JComboBox cbSurvey;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCreatedBy;
    private javax.swing.JLabel lblFormNumber;
    private javax.swing.JLabel lblMainTitle;
    private javax.swing.JLabel lblMainTitle1;
    private java.awt.Label lblQuestionnaires;
    private javax.swing.JLabel lblRefPeriod;
    private javax.swing.JLabel lblSubTitle;
    private javax.swing.JLabel lblSurvey;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JTextField txtCreatedBy;
    private javax.swing.JTextField txtSubTitle;
    private javax.swing.JTextField txtTitle;
    // End of variables declaration//GEN-END:variables


    /**
     * When this window is opened, go get the Questionnaires top component and
     * set up a lookup listener in case this is changed.
     */
    @Override
    public void componentOpened()
    {
        result = WindowManager.getDefault().findTopComponent( "IQuestionnairesTopComponent" )
                .getLookup().lookupResult( Questionnaires.class );
        result.addLookupListener( this );
        resultChanged( new LookupEvent( result ) );
    }


    /**
     * When this is closed, remove the lookup listener.
     */
    @Override
    public void componentClosed()
    {

        result.removeLookupListener( this );
        result = null;
    }


    void writeProperties( java.util.Properties p )
    {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty( "version" , "1.0" );
        // TODO store your settings
    }


    void readProperties( java.util.Properties p )
    {
        String version = p.getProperty( "version" );
        // TODO read your settings according to their version
    }


    /**
     * Set up the combo boxes with all surveys and the Ref periods and form
     * numbers for the first survey.
     */
    void initComboBoxes()
    {

        List<String> surveys = QueryManager.getAllSurveys();
        cbSurvey.setModel( new DefaultComboBoxModel( surveys.toArray() ) );
        String topSurvey = surveys.get( 0 );

        // init Ref Periods
        List<String> repPeriods = QueryManager.getRefPeriods( topSurvey );

        // here's the pesky ReportingPeriods vs String thing again
        List<ReportingPeriods> rps = new ArrayList<>();
        List<String> refPeriods = new ArrayList<>();
        boolean useRPs = false;
        for ( Object o : repPeriods )
        {
            if ( o instanceof ReportingPeriods )
            {
                useRPs = true;
                ReportingPeriods rp = (ReportingPeriods) o;
                rps.add( rp );
            }
            else if ( o instanceof String )
            {
                refPeriods.add( (String) o );
            }
        }

        // init Form Numbers
        List<String> formNumbers;
        if ( useRPs )
        {
            ReportingPeriods emptyRP = new ReportingPeriods();
            emptyRP.setRefPeriod( "" );
            rps.add( rps.size() , emptyRP );

            cbRefPeriod.setModel( new DefaultComboBoxModel( rps.toArray() ) );

            ReportingPeriods topRP = rps.get( 0 );
            formNumbers = QueryManager.getFormNumbers( topSurvey , topRP ); // this has two signatures for String & RP
        }
        else // Strings
        {
            cbRefPeriod.setModel( new DefaultComboBoxModel( refPeriods.toArray() ) );

            String topRefPeriod = refPeriods.get( 0 );
            formNumbers = QueryManager.getFormNumbers( topSurvey , topRefPeriod ); // this has two signatures for String & RP
        }

        formNumbers.add( formNumbers.size() , "" ); // an empty one to search for all Form Numbers
        cbFormNumber.setModel( new DefaultComboBoxModel( formNumbers.toArray() ) );
    }


    /**
     * If the Opening Top Component changes
     *
     * @param le -- LookupEvent
     */
    @Override
    public void resultChanged( LookupEvent le )
    {
        Lookup.Result r = (Lookup.Result) le.getSource();
        Collection<Questionnaires> coll = r.allInstances();
        if ( !coll.isEmpty() )
        {
            for ( Questionnaires q : coll )
            {
                questionnaires = q;                    // the default questionnaire
                txtTitle.setText( q.getTitle() );
                txtSubTitle.setText( q.getSubTitle() );
                txtCreatedBy.setText( q.getCreatedBy() );
                cbSurvey.setSelectedItem( q.getSurvey() );
                cbRefPeriod.setSelectedItem( q.getRefPeriod() );
                cbFormNumber.setSelectedItem( q.getFormNumber() );
            }
        }
        else
        {
            // ???
        }
    }


    @Override
    public UndoRedo getUndoRedo()
    {
        return urMgr;
    }


    /**
     *
     * @param tc
     */
    private void openTC( TopComponent tc )
    {
        tc.open();
        tc.requestActive();
    }

}
